layui.use(['jquery','laydate','form','layedit','upload','layer'],function () {
    var $ = layui.$;
    var laydate = layui.laydate;
    var form = layui.form;
    var layedit = layui.layedit;
    var upload = layui.upload;
    var layer = layui.layer;
    //开始时间渲染
    laydate.render({
        elem: '#startDate',
        type: 'datetime'
    });
    $(document).click(function () {
        parent.$('.tab').hide();
    });
    //到期时间渲染
    laydate.render({
        elem: '#expireDate',
        type: 'datetime'
    });
    //返回按钮
    $('#goBack').click(function () {
        var title = $('#title').text();
        var iframe = parent.$('.iframe');
        $.each(iframe,function (i,item) {
            if(title == $(item).attr('_href')){
                parent.$('#beforeHref').val($(item).attr('_href'));
                parent.beforeHref();
                return;
            }
        });
    });
    $('#refresh').click(function () {
        location.reload();
    });
    //创建人文本框渲染
    var user = JSON.parse(localStorage.getItem('user'));
    $('#createId').val(user.data.userId);
    //上传文件渲染
    upload.render({
        elem: '#uploadImage',
        url: '/JsCarousel/uploadImagesCarousel',
        accept: 'images',
        field: 'file',
        method: "post",
        exts: 'png|jpg|gif',
        done: function(res, index, upload){
            if(res.code == 0){
                layer.msg('上传成功');
                $('#imgAddress').val(res.data.data.src);
                $('#imgType').val(res.data.data.imgType);
                $('#currentImage').attr('src',res.data.data.src);
            }
        }
    });
    //提交按钮
    form.on('submit(sub)',function (obj) {
        $.ajax({
            url: '/JsCarousel/addJsCarousel',
            data: obj.field,
            type: 'post',
            dataType: 'json',
            success: function (data) {
                if(data.data.carouselId != '' && data.data.carouselId != null){
                    $('input,textarea').val('');
                    $('img').attr('src','');
                    parent.$('#quickEntry').val('轮播图列表');
                    parent.quickEntry();
                }else{
                    layer.msg('添加失败');
                }
            }
        });
        return false;
    });
});